package com.example.demoes.web;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demoes.domain.Test;
import com.example.demoes.exception.BusinessException;
import com.example.demoes.mapper.TestMapper;
import com.example.demoes.message.MessageContent;
import com.example.demoes.message.MessageTopic;
import com.example.demoes.result.BusinessResponse;
import com.example.demoes.service.TestService;
import com.example.demoes.service.TestSlaveService;
import com.example.demoes.utils.MsgSenderTemplateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class TestController {

    private final TestService testService;
    private final TestMapper testMapper;
    private final TestSlaveService testSlaveService;
    private final MsgSenderTemplateUtils msgSenderTemplateUtils;

    @GetMapping("/printLog")
    public String printLog() {
        log.info("printLog: {}", DateTime.now());
        return "";
    }
    @GetMapping("/testEx")
    public BusinessResponse testException() {
        try {
            int a = 1 / 0;
        } catch (Exception e) {
            throw new RuntimeException("test");
        }
        return BusinessResponse.success(null);
    }

    @GetMapping("/testMaster")
    public BusinessResponse testMaster() {
        log.info("testMaster: {}", DateTime.now());
        return BusinessResponse.success(testService.lambdaQuery().list());
    }
    @GetMapping("/testSlave")
    public BusinessResponse testSlave() {
        log.info("testMaster: {}", DateTime.now());
        return BusinessResponse.success(testSlaveService.lambdaQuery().list());
    }

    @GetMapping("/testMessage")
    public BusinessResponse testMessage() {
        log.info("testMaster: {}", DateTime.now());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",1);
        MessageContent messageContent = new MessageContent("1", MessageTopic.TEST,jsonObject);
        MsgSenderTemplateUtils.sendEventMessage(messageContent);
        return BusinessResponse.success("mq发送成功！");
    }
    @GetMapping("/testPage")
    public BusinessResponse testPage() {
        log.info("testMaster: {}", DateTime.now());
        QueryWrapper<Test> wrapper = new QueryWrapper<>();

        // 创建分页对象（1表示第一页；4表示每页大小为4）
        Page<Test> page = new Page<>(1, 4);
        Page<Test> result = testMapper.selectPage(page, wrapper);
        System.out.println("page == result: " + (page == result));
        System.out.println("size: " + result.getSize());
        System.out.println("total: " + result.getTotal());
        for(Test test : result.getRecords()) {
            System.out.println(test);
        }
        return BusinessResponse.success("分页查询成功！");
    }


}
